{% extends "master.html" %}
{% block title %}Users{% endblock %}
{% set tag = "users" %}
{% from "_browseheader.html" import browse_header %}

{% block header %}
<link href="{{ url_for('static', filename='selectize.bootstrap3.css') }}"
  rel="stylesheet" />
{% endblock %}

{% block content %}
<div class="repo-header p-t-1">
  <div class="container">
    {{browse_header(select=select)}}
  </div>
</div>

<div class="container">
  {% if total_page and total_page > 1 %}
  <nav class="text-center">
    <ul class="pagination">
      <li {% if page <= 1%} class="disabled" {% endif %}>
      <a href="{{ url_for('view_users') }}?page={{page - 1}}" aria-label="Previous">
          <span aria-hidden="true">&laquo;</span>
          <span class="sr-only">Newer</span>
        </a>
      </li>
      <li class="active">page {{ page }} of {{total_page}}</li>
      <li {% if page >= total_page %}class="disabled"{%endif%}>
        <a href="{{ url_for('view_users') }}?page={{page + 1}}" aria-label="Next">
          <span aria-hidden="true">&raquo;</span>
          <span class="sr-only">Older</span>
        </a>
      </li>
    </ul>
  </nav>
  {% endif %}

  <section class="user_list p-t-2">
    <h2 class="m-b-1">
      Users <span class="label label-default">{{ users_length }}</span>
      {%- if authenticated and config.get('PAGURE_AUTH') == 'local' %}
      <a href="{{ url_for('new_user') }}">
      <button type="button" class="btn btn-success pull-xs-right">
          New User
      </button>
      </a>
      {% endif -%}
    </h2>
    {% for user in users %}
      <div class="col-lg-4 col-md-6">
        <div class="project_wrapper m-b-1">
          <img class="projecticon pull-xs-left" src="{{
            user.default_email | avatar_url(60) |safe }}" width=60 height=60 />
          <a class="project_link logo_link" href="{{ url_for('view_user', username=user.user)}}">
            <div class="repo_name"><strong>{{ user.fullname }}</strong></div>
          </a>
          <div class="repo_desc">
            <small>{{ user.user }} joined <span title="{{
            user.created.strftime('%Y-%m-%d %H:%M:%S') }}">{{
            user.created | humanize }}</span></small>
          </div>
          <div class="project_metadata">
            <div style="text-align:right;" class="p-r-1 text-muted">
              <span title="Projects" data-toggle="tooltip">
                <span class="oi" data-glyph="document"></span>
                {{- user.repos_length}} &nbsp;
              </span>
              <span title="Forks" data-toggle="tooltip">
                <span class="oi" data-glyph="fork"></span>
                {{- user.forks_length}} &nbsp;
              </span>
              <span title="Groups" data-toggle="tooltip">
                <span class="oi" data-glyph="people"></span>
                {{- user.groups|count}}
              </span>
            </div>
          </div>
        </div>
      </div>
    {% endfor %}
  </section>

  {% if total_page and total_page > 1 %}
  <nav class="text-center">
    <ul class="pagination">
      <li {% if page <= 1%} class="disabled" {% endif %}>
      <a href="{{ url_for('view_users') }}?page={{page - 1}}" aria-label="Previous">
          <span aria-hidden="true">&laquo;</span>
          <span class="sr-only">Newer</span>
        </a>
      </li>
      <li class="active">page {{ page }} of {{total_page}}</li>
      <li {% if page >= total_page %}class="disabled"{%endif%}>
        <a href="{{ url_for('view_users') }}?page={{page + 1}}" aria-label="Next">
          <span aria-hidden="true">&raquo;</span>
          <span class="sr-only">Older</span>
        </a>
      </li>
    </ul>
  </nav>
  {% endif %}
</div>

{% endblock %}

{% block jscripts %}
    {{ super() }}
<script src="{{ url_for('static', filename='selectize.min.js') }}" type="text/javascript"> </script>
<script type="text/javascript">
$(document).ready(function() {
  $('#headerSearch').on('keypress keydown keyup', function(e) {
    if (e.which == 13) {
        e.preventDefault();
        return false;
    }
    });
  $('#term').selectize({
    valueField: 'user',
    labelField: 'user',
    searchField: 'user',
    onType: function(value){
      if (value == ""){
      this.close();
      }
    },
    onChange: function(value){
      if (value != ""){
        $('#headerSearch').submit();
      }
    },
    maxItems: 1,
    create: false,
    load: function(query, callback) {
      if (!query.length) return callback();
      $.getJSON(
        "{{ url_for('api_ns.api_users') }}", {
          pattern: query
        },
        function( data ) {
          callback( data.users.map(function(x) { return { user: x }; }) );
        }
      );
    },
    render: {
      option: function(item, escape) {
        return '<div>' +
          '<div class="projecticon-search pull-xs-left"><span class="oi" data-glyph="person"></span></div>'+
          '<div class="title">' +
            '<span class="name"><strong>' + escape(item.user) + '</strong></span>' +
          '</div>' +
          '<div class="description"><small>' + escape(item.user) + '</small></div>' +
        '</div>';
      }
    },
  });
});
</script>
{% endblock %}
